<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 23:02:53
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_buybooks extends discuz_table
{

    public function __construct()
    {
        $this->_table = 'jamesonread_buybooks';
        $this->_pk    = 'id';
        $this->_pre_cache_key = 'jamesonreadbuybook_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    // 根据uid和book_id判断用户是否购买过本书
    public function hasBuyBook($buy_id, $book_id)
    {
        return (int) DB::result_first('SELECT count(buy_id) FROM %t WHERE buy_id=%d AND book_id=%d', array($this->_table, $buy_id, $book_id));
    }
    // 当前销售收入
    public function fetch_sale_byuid($uid)
    {
        return (int) DB::result_first('SELECT SUM(price) FROM %t WHERE author_id=%d', array($this->_table, $uid));
    }
    public function count_sale_bybookid($book_id)
    {
        return (int) DB::result_first('SELECT SUM(price) FROM %t WHERE book_id=%d', array($this->_table, $book_id));
    }
    public function fetch_books_by_uid($uid)
    {
        return DB::fetch_all('SELECT b.book_id,b.book_name,b.category_id,b.uid,b.parent_id,b.image,b.shuxing,buy.buy_id,buy.price as money,buy.addtime FROM %t AS b INNER JOIN %t AS buy ON b.book_id=buy.book_id WHERE buy.buy_id=%d', array('jamesonread_books', $this->_table, $uid));
    }
    public function fetch_books_by_authorid($uid)
    {
        return DB::fetch_all('SELECT b.book_id,b.book_name,b.category_id,b.uid,b.parent_id,b.image,b.shuxing,c.username,buy.buy_id,buy.price,buy.addtime FROM %t AS b INNER JOIN %t AS buy ON b.book_id=buy.book_id INNER JOIN %t AS c ON buy.buy_id=c.uid WHERE buy.author_id=%d', array('jamesonread_books', $this->_table, 'common_member',$uid));
    }
    public function fetch_by_page($uid,$start,$size)
    {
        $cacheid = 'fetch_by_page-'.$uid.'-'.$start.'-'.$size;
        if($cache = $this->fetch_cache($cacheid)){
            return $cacheid;
        }
        $data = DB::fetch_all('SELECT b.book_id,b.book_name,b.category_id,b.uid,b.parent_id,b.image,b.shuxing,y.buy_id,y.price as money,y.addtime FROM %t AS b INNER JOIN %t AS y ON b.book_id=y.book_id WHERE y.buy_id=%d ORDER BY y.addtime DESC LIMIT %d,%d', array( 'jamesonread_books',$this->_table, $uid, $start, $size));
        $this->store_cache($cacheid,$data);
        return $data;
    }
    public function count_by_uid($uid)
    {
        if($cache = $this->fetch_cache('countbyuid_'.$uid)){
            return $cache;
        }
        $data = (int) DB::result_first('SELECT count(book_id) FROM %t WHERE buy_id=%d', array($this->_table, $uid));
        $this->store_cache('countbyuid_'.$uid,$data);
        return $data;
    }
    public function count_by_authorid($author_id)
    {
        if($cache = $this->fetch_cache('countbyauthorid_'.$author_id)){
            return $cache;
        }
        $data = (int) DB::result_first('SELECT count(book_id) FROM %t WHERE author_id=%d', array($this->_table, $author_id));
        $this->store_cache('countbyauthorid_'.$author_id,$data);
        return $data;
    }
    public function count_by_bookid($book_id)
    {
        if($cache = $this->fetch_cache('countbybookid_'.$book_id)){
            return $cache;
        }
        $data = (int) DB::result_first('SELECT count(book_id) FROM %t WHERE book_id=%d', array($this->_table, $book_id));
        $this->store_cache('countbybookid_'.$book_id,$data);
        return $data;
    }
    public function fetch_jilu_by_bookid($book_id,$start=0,$size=30)
    {
        $limit = ' LIMIT %d,%d';
        if(!$size){
            $limit = '';
        }
        return DB::fetch_all('SELECT b.book_id,b.book_name,b.category_id,b.uid,b.parent_id,b.image,b.shuxing,c.username,buy.buy_id,buy.price,buy.addtime FROM %t AS b INNER JOIN %t AS buy ON b.book_id=buy.book_id INNER JOIN %t AS c ON buy.buy_id=c.uid WHERE buy.book_id=%d '.$limit, array('jamesonread_books', $this->_table, 'common_member',$book_id,$start,$size));
    }
}